package com.saifan.wyy_ov.ui.property;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.RepairBean;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.LabelView;
import com.saifan.wyy_ov.ui.view.RefreshLayout;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.DensityUtil;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ToastUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-05-08 12:47
 * @descrption 投诉建议记录
 */
public class ComplaintandadviceRecordActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private Toolbar toolbar;
    private int page = 0;

    int type = 2;
    private CommonAdapter<RepairBean> adapter;
    private List<RepairBean> beans = new ArrayList<>();
    private ListView listView;
    private RefreshLayout refreshLayout;
    private View loadlay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_recoder);
        loadlay = findViewById(R.id.loading_lay);
        this.refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        this.listView = (ListView) findViewById(R.id.listView);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initAdapter();
        query();

    }

    public void initAdapter() {
        adapter = new CommonAdapter<RepairBean>(ComplaintandadviceRecordActivity.this, beans, R.layout.repair_recoder_item) {
            @Override
            public void convert(ViewHolderHelper helper, RepairBean item) {
                helper.setText(R.id.SJ, StringUtil.returnShow(item.getWYBSQD_FSSJ()));
                helper.setText(R.id.BT, StringUtil.returnShow(item.getWYBSQD_BSLX()));
                helper.setText(R.id.LR, StringUtil.returnShow(item.getWYBSQD_BSLR()));
                helper.setText(R.id.BM, "处理部门：" + StringUtil.returnShow(item.getWYBSQD_GWMC()));
                LinearLayout targetView = helper.getView(R.id.layout);
                int window_width_dp = ComplaintandadviceRecordActivity.this.getWindowManager().getDefaultDisplay().getWidth();
                int window_width_px = DensityUtil.px2dip(ComplaintandadviceRecordActivity.this, window_width_dp) - 20;
                LabelView label = helper.getView(12345);
                if (label == null) {
                    label = new LabelView(ComplaintandadviceRecordActivity.this);
                    label.setId(12345);
                    label.setHeight(DensityUtil.dip2px(ComplaintandadviceRecordActivity.this, 40));
                    label.setWidth(DensityUtil.dip2px(ComplaintandadviceRecordActivity.this, 120));
                    label.setGravity(Gravity.BOTTOM | Gravity.CENTER);

                }
                if (item.isWYBSQD_SFJD() && !item.isWYBSQD_SFHF() && !item.isWYBSQD_SFWC()) {


                    label.setBackgroundColor(getResources().getColor(R.color.lable2));
                    label.setTargetViewInBaseAdapter(targetView, window_width_px, 0, LabelView.Gravity.RIGHT_TOP);
                    label.setText("处理中");


                } else if (item.isWYBSQD_SFWC() && !item.isWYBSQD_SFHF()) {
                    label.setBackgroundColor(getResources().getColor(R.color.lable3));
                    label.setTargetViewInBaseAdapter(targetView, window_width_px, 0, LabelView.Gravity.RIGHT_TOP);
                    label.setText("已完成");
                } else if (item.isWYBSQD_SFHF()) {
                    label.setBackgroundColor(getResources().getColor(R.color.lable1));
                    label.setTargetViewInBaseAdapter(targetView, window_width_px, 0, LabelView.Gravity.RIGHT_TOP);
                    label.setText("已回访");
                } else {
                    label.setBackgroundColor(getResources().getColor(R.color.state3));
                    label.setTargetViewInBaseAdapter(targetView, window_width_px, 0, LabelView.Gravity.RIGHT_TOP);
                    label.setText("已受理");
                }


            }
        };
        listView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // TODO Auto-generated method stub
                page = 0;
                refreshLayout.setRefreshing(true);
                query();
            }
        });

        refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                // TODO Auto-generated method stub
                refreshLayout.setLoading(true);
                page++;
                query();
            }
        });

        listView.setOnItemClickListener(this);
    }

    public void query() {
        GetData getData = new GetData();
        if (beans.isEmpty()) {
            loadlay.setVisibility(View.VISIBLE);
        }
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("FKGX_ZJ", getRoomInfoBean().getFKGX_ZJ());
            jsonObject.put("Page", page);
            jsonObject.put("Type", type);
            getData.getDataJson(ComplaintandadviceRecordActivity.this, Api.Matter.MATTER_INFO, jsonObject, "", new ResponseData() {
                @Override
                public void onSuccess(String json) {

                    List<RepairBean> list = new Gson().fromJson(json, new TypeToken<List<RepairBean>>() {
                    }.getType());
                    if (list != null && !list.isEmpty()) {
                        if (page == 0) {
                            beans = list;
                        } else {
                            beans.addAll(list);
                        }
                        adapter.mDatas = beans;
                        adapter.notifyDataSetChanged();

                    } else {
                        if (page > 0) {
                            ToastUtil.showToast(ComplaintandadviceRecordActivity.this, "信息已全部显示完毕！");
                            page--;
                        } else {

                            beans.clear();
                            ToastUtil.showToast(ComplaintandadviceRecordActivity.this, "暂无更多信息");
                        }

                    }

                    refreshLayout.setRefreshing(false);
                    refreshLayout.setLoading(false);
                    loadlay.setVisibility(View.GONE);


                }

                @Override
                public void onFailed(String str) {

                    refreshLayout.setRefreshing(false);
                    refreshLayout.setLoading(false);
                    loadlay.setVisibility(View.GONE);
                    if (page > 0) page--;

                }
            });

        } catch (JSONException e) {


        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent(this, ProgressItemActivity.class);
        intent.putExtra("data", beans.get(position));
        startActivity(intent);

    }
}
